'use strict';

const Controller = require('egg').Controller;
const OSS = require('ali-oss');
const sha256 = require('crypto-js/sha256');

const client = new OSS({
  region: 'oss-cn-beijing',
  accessKeyId: 'LTAI4GJ8oQjn5Aort5zxte5v',
  accessKeySecret: 'yacZQiKaoK12o09lmhNT9HTSnSvwMu',
  bucket: 'bwcreation'
});

class OssController extends Controller {
  async index() {
    const { ctx } = this;
    let result = await client.listBuckets({
      "max-keys": 10
    })
    ctx.body = result;
  }
  async create(){
    const { ctx } = this;
    const stream = await ctx.getFileStream();
    let tempArr = stream.fieldname.split('.');
    const name = sha256(stream.fieldname).toString()+'.'+tempArr[tempArr.length-1];
    console.log('name...', name);
    let result = await client.put(name, stream);
    ctx.body = result;
  }
}

module.exports = OssController;
